package rock.loadbalance.impl;

import rock.loadbalance.AbstractLoadBalance;
import rock.remoting.dto.RpcRequest;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class ThreadLocalRandomLoadBalance extends AbstractLoadBalance {
    /**
     * 使用ThreadLocalRandom，保证每个线程自己存自己的Seed，并发情况下好一些
     * @param serviceAddresses 可选list
     * @param request rpc request
     * @return
     */
    @Override
    protected String doSelect(List<String> serviceAddresses, RpcRequest request) {
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        return serviceAddresses.get(rand.nextInt(serviceAddresses.size()));
    }
}
